using System;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace DesertPaintLab
{
	public class Palette
	{
		static List<PaintColor> colors = new List<PaintColor>();
		static Regex colorEntry = new Regex(@"\#(?<red>\w\w)(?<green>\w\w)(?<blue>\w\w)\s*(?<name>\w+)");

		
		public Palette ()
		{
			
		}
		
		public static void Load(string file)
		{
			string line;
			Match match;
			using (StreamReader reader = new StreamReader(file))
			{
				while ((line = reader.ReadLine()) != null) 
                {
					match = colorEntry.Match(line);
					if (match.Success)
					{
						colors.Add(new PaintColor(match.Groups["name"].Value,
						                          match.Groups["red"].Value,
						                          match.Groups["green"].Value,
						                          match.Groups["blue"].Value));
					}
                }
			}
		}
		
		public static string FindNearest(PaintColor color)
		{
			int bestDistSq = int.MaxValue;
			PaintColor bestColor = null;
			foreach (PaintColor paintColor in colors)
			{
				int distSq = paintColor.GetDistanceSquared(color);
				if (distSq < bestDistSq)
				{
					bestDistSq = distSq;
					bestColor = paintColor;
				}
			}
			return bestColor.Name;
		}
	}
}

